package com.xrtn.utils;


import com.xrtn.bean.SysUserInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

/**
 * 对象相关的基础工具类
 * @author zhengchb
 */
public class ObjUtils {
	private static Random random = new SecureRandom();
	/**
	 * obj is empty Object
	 * @param obj
	 * @return
	 */
	public static boolean isEmpty(Object obj){
		return (obj == null) || "".equals(obj);
	}
	
	/**
	 * obj is not empty Object
	 * @param obj
	 * @return
	 */
	public static boolean isNotEmpty(Object obj){
		return !isEmpty(obj);
	}

	/**
	 * obj is an array class
	 * @param obj
	 * @return
	 */
	public static boolean isArray(Object obj) {
		return (obj != null) && (obj.getClass().isArray());
	}

	/**
	 * Object[] is empty
	 * @param array
	 * @return
	 */
	public static boolean isEmpty(Object[] array) {
		return (array == null) || (array.length == 0);
	}
	
	/**
	 * Object[] is empty
	 * @param array
	 * @return
	 */
	public static boolean isEmpty(String[] array) {
		return (array == null) || (array.length == 0);
	}
	
	/**
	 * Object[] is not empty
	 * @param array
	 * @return
	 */
	public static boolean isNotEmpty(Object[] array) {
		return !isEmpty(array);
	}
	
	/**
	 * Object[] is not empty
	 * @param array
	 * @return
	 */
	public static boolean isNotEmpty(String[] array) {
		return !isEmpty(array);
	}

	/**
	 * 判断某对象是否为Boolean且值为真
	 * @param value
	 * @return
	 */
	public static boolean getBoolean(Object value) {
		if (value instanceof Boolean) {
			return ((Boolean) value).booleanValue();
		} else {
			return false;
		}
	}
	
	/**
	 * obj is empty Object return null
	 * @param <T>
	 * @param obj
	 * @return
	 */
	public static <T> T isEmptyReturnNull(T obj){
		if(isEmpty(obj)){
			return null;
		}
		return obj;
	}
	
	/**
	 * obj is empty Object return null
	 * @param obj
	 * @return
	 */
	public static String[] isEmptyReturnNull(String[] obj){
		if(isEmpty(obj)){
			return null;
		}
		return obj;
	}
	public static String randomNumeric(Integer len) {
		return random
				.ints(1, 1, 10)
				.mapToObj(String::valueOf)
				.reduce((s, s1) -> s + s1).get() +
				random
				.ints(len - 1, 0, 10)
				.mapToObj(String::valueOf)
				.reduce((s, s1) -> s + s1).get();
	}
	public static String getNowDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

    /**
     * 获取订单👌
     * @return
     */
	public static String getOrderNo(){
        return "GYT"+ObjUtils.getNowDate() + "1" + ObjUtils.randomNumeric(6);
    }

    /**
     * 获取账务流程编号
     * @return
     */
    public static String getAccountNo(){
        return "GYT"+ObjUtils.getNowDate() + "2" + ObjUtils.randomNumeric(6);
    }

    /**
     * 获取病程编号
     * @return
     */
    public static String getDegreeNo(){
        return "GYT"+ObjUtils.getNowDate() + "3" + ObjUtils.randomNumeric(6);
    }

    /**
     * 账户自动
     * @param userCount
     * @return
     */
    public static String getUserNo(String userCount){

	    String userNo = "GYT100001";

        userNo = userNo.substring( 0 , userNo.length()-userCount.length() ) + userCount;

        System.out.println("userNo = " + userNo);

        return userNo;
    }

    public static Map<String,Object> putSubclinicId(SysUserInfo userInfo,Map<String,Object> params,Integer subclinicId){

        JSONArray jsonArray = JSONArray.fromObject(userInfo.getRoleFunction());

        for (int i = 0 ; i < jsonArray.size() ; i ++){
            JSONObject obj = jsonArray.getJSONObject(i);
            Integer sysFunction = obj.optInt("menuId");
            if(sysFunction != SysConstant.IS_SHOW_ALL_SUBCLINIC){
                params.put("subclinicId", subclinicId);
                break;
            }
        }
        return params;
    }

    /**
     * 校验参数
     * @param obj
     * @return
     */
    public static CodeEnum validateParams(Object [] obj){
        for (Object object : obj){
            if(obj == null || obj.equals("")){
                //设置返回数据
                return CodeEnum.CODE_90001;
            }
        }
        return null;
    }
    public static void main(String args[]){
        getUserNo("12");
    }
}
